﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using INTEX.DataBase.Tools;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{    
    public partial class JLTRP02Data : BaseFunction
    {
        ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
        INTEXDBC INTEXDBC
        {
            get
            {
                return app.INTEXDBC;
            }
        }
        /*public INTEXBL INTEXBL
        {
            get
            {
                return app.INTEXBL;
            }
        }

        SETINTEXDB INTEXDB = new SETINTEXDB();
         */
        public JLTRP02Data()
            : base()
        {
        }
         public JLTRP02Data(Dictionary<string, object> DicGlobalPara)
            : base(DicGlobalPara)
        {
        }

        public DataSet GetRP02(DataRow dwTABEL)
        {
            DataSet Result;
            DataSet Return = new DataSet();
            string TableName = "RP02";
            string CO = dwTABEL["CO"].ToString().Trim();
            string MACHINE = dwTABEL["MACHINE"].ToString().Trim();
            try
            {
                orlReadCommand = INTEXDBC.OracleDB.CreateCommand();
                orlReadCommand.CommandText = "SELECT * FROM (SELECT TO_NUMBER(AMLFDNR) AS MACHINE,NVL(B.AMTDAT,0) AS FROMDATE, "
                    + " SUBSTR(LPAD(NVL(B.AMTZT,0),8,0),1,2)||':'||SUBSTR(LPAD(NVL(B.AMTZT,0),8,0),3,2) AS FROMTIME, "
                    + " (SELECT TRIM(X.ABPART)||'-'||TRIM(X.ABPFARBE) FROM ABPOS X "
                    + " WHERE X.ABPMANDANT = B.AMTMANDANT AND X.ABPAUFKRS = B.AMTAUFKRS "
                    + " AND X.ABPAUFLFD = B.AMTAUFLFD AND X.ABPAUFPOS = B.AMTAUFPOS ) AS ARTICLE, "
                    + " (SELECT X.ABPZUGET*0.01 FROM ABPOS X "
                    + " WHERE X.ABPMANDANT = B.AMTMANDANT AND X.ABPAUFKRS = B.AMTAUFKRS "
                    + " AND X.ABPAUFLFD = B.AMTAUFLFD AND X.ABPAUFPOS = B.AMTAUFPOS ) AS QTY, "
                    + " DECODE(A.AMBELKZ,'J',B.AMTBAUMKRS1||'-'||TO_NUMBER(B.AMTBAUMLFD1),'') AS BEAM, "
                    + " DECODE(A.AMBELKZ,'J',B.AMTAUFKRS||'-'||TO_NUMBER(B.AMTAUFLFD)||'-'||TO_NUMBER(B.AMTAUFPOS),'') AS ORD "
                    + " FROM AMSTAMM A,AMAKT B "
                    + " WHERE A.AMNRKRS = 'M ' AND A.AMART = 'WEBMA   ' "
                    + " AND A.AMMANDANT = B.AMTMANDANT(+) AND A.AMNRKRS = B.AMTMNRKRS(+) "
                    + " AND A.AMLFDNR = B.AMTMLFDNR(+) AND A.AMPOS = B.AMTMPOS(+) "
                    + " AND (TO_NUMBER(AMLFDNR) = '"+MACHINE+"' OR '"+MACHINE+"'='AAAA' ) "
                    + " ) WHERE NVL(ARTICLE,'-') LIKE '"+CO+"%'   ORDER BY MACHINE "
                    ;

                Result = INTEXDBC.Connect2Proxy(orlReadCommand);
                if (Result.Tables.Count > 0)
                {
                    Result.Tables[0].TableName = TableName;
                    //byte[] b;
                    //string value;
                    //foreach (DataRow dw in Result.Tables[TableName].Rows)
                    //{
                    //    b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dw["REMARK"].ToString().Trim());
                    //    value = Encoding.GetEncoding("gb2312").GetString(b);
                    //    dw["REMARK"] = value;
                       
                    //}
                }
                Return.Merge(Result);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return Return;
        }
        
        public DataSet BuildData(DataRow dwUI)
        {
            /*DataSet SResult = new DataSet();
            SETINTEXDB SReturn = new SETINTEXDB(); */
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                setResult = GetRP02(dwUI);
                setReturn.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }

    }
}